<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Http\Controllers\Controller;
use App\Services\IndexService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ArticleController extends Controller
{
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('文章管理')->description('文章列表')->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content->header('查看文章')->description('详情')->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		return $content->header('编辑文章')->description('编辑')->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content->header('创建文章')->description('创建文章')->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Article);

		$grid->id('Id');
		$grid->title('标题');
		$grid->description('描述')->limit(30);
		$grid->column('image','封面图')->image();
		$grid->is_star('是否推荐')->radio(Article::RECOMMEND_MAP);
		$grid->is_release('是否发布')->radio(Article::RELEASE_MAP);
		$grid->real_pv('浏览量');
		$grid->column('pv', '假浏览量')->editable();
		$grid->created_at('创建时间');
		$grid->updated_at('编辑时间');

		$grid->tools(function(Tools $tools) use ($grid) {
			$tools->batch(function(BatchActions $batch) use ($grid) {
				$batch->disableDelete();
			});
		});

		$grid->actions(function(Grid\Displayers\Actions $actions) {
			$actions->disableDelete();
		});

		$grid->disableExport();
		$grid->disableRowSelector();

		$grid->filter(function(Filter $filter) {

			// 去掉默认的id过滤器
			$filter->disableIdFilter();

			// 在这里添加字段过滤器
			$filter->like('title', '标题');
		});

		$grid->model()->orderBy('is_star', 'desc')->orderBy('is_release', 'desc')->orderBy('id', 'desc');

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 *
	 * @return Show
	 */
	protected function detail($id)
	{
		$show = new Show(Article::findOrFail($id));

		$show->id('Id');
		$show->title('标题');
		$show->description('描述');
        $show->image('封面')->image();
//        $show->video('视频')->videl();
//        $show->video_cover('视频封面')->image();
		$show->details('详细');
		$show->is_star('是否是推荐文章')->using(Article::RECOMMEND_MAP);
		$show->is_release('是否发布')->using(Article::RELEASE_MAP);
		$show->seo_title('SEO标题');
		$show->seo_description('SEO描述');
		$show->seo_keyword('SEO关键字');
        $show->real_pv('浏览量');
		$show->pv('假浏览量');
		$show->created_at('创建时间');
		$show->updated_at('编辑时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$form = new Form(new Article);

		$form->text('title', '标题')->required();
		$form->textarea('description', '描述')->rules('required|max:198', [
            'max'   => '最多198个字符',
        ]);
		$form->image('image', '封面图片')->uniqueName()->required()->help('建议尺寸:1:1(图片大小在线压缩网站：https://imagecompressor.com/zh/)');
//        $form->divider('如果上传了视频一定要上传视频封面图');
//		$form->largefile('video', '视频');
//		$form->image('video_cover', '视频封面');
		$form->divider();
		$form->editor('details', '详细')->required();
		$form->radio('is_star', '是否推荐')->options(Article::RECOMMEND_MAP)->default('0');
		$form->radio('is_release', '是否发布')->options(Article::RELEASE_MAP)->default('0');
		$form->text('seo_title','SEO标题')->required();
		$form->text('seo_description','SEO描述')->required();
		$form->text('seo_keyword','SEO关键字')->required();
		$form->number('pv', '假浏览量')->default(0);

		$form->tools(function(Form\Tools $tools) {
			$tools->disableDelete();
			$tools->disableView();
		});

        $form->saved(function (Form $form){
            IndexService::indexCache();
        });

		return $form;
	}
}
